package com.tddx.activiti.business.core.controller;

import org.apache.log4j.Logger;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.beans.TypeMismatchException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.NativeWebRequest;

@ControllerAdvice
public class ExceptionController {
	
	Logger log = Logger.getLogger(this.getClass());
	
	@ExceptionHandler
	public String requestTypeMismatch(NativeWebRequest request, Exception ex){
		if(ex instanceof TypeMismatchException){
			TypeMismatchException tmpex = (TypeMismatchException)ex;
			request.setAttribute("msg", "参数类型不匹配, 参数:" + tmpex.getPropertyName(), NativeWebRequest.SCOPE_REQUEST);
			log.error("参数类型不匹配,参数:" + tmpex.getPropertyName() + "类型应该为" + tmpex.getRequiredType());
		}else if(ex instanceof MissingServletRequestParameterException){
			MissingServletRequestParameterException tmpex = (MissingServletRequestParameterException)ex;
			request.setAttribute("msg", "缺少必要参数, 参数名称为" + tmpex.getParameterName(), NativeWebRequest.SCOPE_REQUEST);
			log.error("缺少必要参数, 参数名称为" + tmpex.getParameterName());
		}else if(ex instanceof AuthorizationException){
			log.error("您没有权限访问!");
			request.setAttribute("msg", "您没有权限访问!", NativeWebRequest.SCOPE_REQUEST);
		}
		return "/exception/ex";
	}
	
}
